package at.fhooe.stefanirndorfer.raycast;

import at.fhooe.stefanirndorfer.sound.OpenALUtil;
import at.fhooe.stefanirndorfer.util.Vector2D;
import at.fhooe.stefanirndorfer.walls.GeometryManager;


public class Ray {

	private static double STEP_SIZE = 0.5;
	
	/** within 34 meters is everything that reflects in less than 100 ms */
	private static double RAY_RANGE = 34*OpenALUtil.ONE_METER_IN_PX;

	private final Vector2D m_origin, step;
	private final int nSteps;
	private GeometryManager geometryManager;
	

	public Ray(Vector2D origin, Vector2D dir, GeometryManager geometryManager) {
		m_origin = origin;
		step = dir.clone();
		step.normalize().scale(STEP_SIZE);
		nSteps = (int) (RAY_RANGE/STEP_SIZE);
		this.geometryManager = geometryManager;
	}


	public Vector2D hits(){
		Vector2D curPos = m_origin.clone();
		for(int i = 0; i < nSteps; i++){
			if(geometryManager.checkCollisions(curPos.x, curPos.y) != null){
				return curPos;
			}
			curPos.add(step);
		}
		return null;
	}

}
